
/**
 * @functionName importAll
 * @param {Array} r 文件列表
 * @param {Number} type 返回类型 0:数组;1:对象;
 * @return {Array} 返回导出对象
 * @description 模块动态引入，仅限于es6模块; importAll(require.context('.', true, /\.router\.js/))
 * @author 张航
 * @date 2020-04-27 15:45:41
 * @version V1.0.0
 */
export const importAll = (r, type = 0) => {
  const res = type === 1 ? {} : []
  r.keys().map(value => {
    if (type === 0) {
      r(value).default.map(item => {
        res.push(item)
      })
    }
    if (type === 1) {
      Object.assign(res, r(value))
    }
  })

  return res
}

/**
 * @param {el，from，to，duration，endCallback}
 * @description scrollTop animation
 * @author: 王鹏
 * @return {}
 */
export function scrollTop(el, from = 0, to, duration = 500, endCallback) {
  if (!window.requestAnimationFrame) {
    window.requestAnimationFrame =
      window.webkitRequestAnimationFrame ||
      window.mozRequestAnimationFrame ||
      window.msRequestAnimationFrame ||
      function(callback) {
        return window.setTimeout(callback, 1000 / 60);
      };
  }
  const difference = Math.abs(from - to);
  const step = Math.ceil((difference / duration) * 50);

  function scroll(start, end, step) {
    if (start === end) {
      endCallback && endCallback();
      return;
    }

    let d = start + step > end ? end : start + step;
    if (start > end) {
      d = start - step < end ? end : start - step;
    }

    if (el === window) {
      window.scrollTo(d, d);
    } else {
      el.scrollTop = d;
    }
    window.requestAnimationFrame(() => scroll(d, end, step));
  }
  scroll(from, to, step);
}
/**
 * @param {none}
 * @author: 王鹏
 * @return {String}
 */
export const createUuid = () => {
  var s = [];
  var hexDigits = '0123456789abcdef';
  for (var i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = '4';
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = '-';

  var uuid = s.join('');
  return uuid;
}

/// 角色菜单
export const roleMenu = () => {
  let objPath = {
    /*
      Purchase_Level  采购人角色-本级
      Local_Supplier 本地供应商
      Pre_Supplier 预备供应商
      Pre_Purchaser 预备采购人
      Purchaser 采购人
      Admin 管理员角色
      Super_Admin 超级管理员
   */
    'ROLE_Purchase_Level': {path: '/',name:'采购专员'},
    'ROLE_Local_Supplier': {path: '/home',name:'本地供应商管理'},
    'ROLE_Pre_Purchaser' : { path: '/home',name:'预备供应商'}, // 预备供应商
    'ROLE_Pre_Supplier' : { path: '/home',name:'预备采购人'},  // 预备采购人
    'ROLE_Purchaser': {path: '/my-cart', name:'采购管理员'},
    'ROLE_Supplier': {path: '/home',name:'供应商管理'},
    'ROLE_Admin':{path: '/home',name:'管理员'},
    'ROLE_Super_Admin' : { path: '/home',name:'超级管理员'},   // 超级管理员
    //
    // '中心': { path: '/home',name:'中心管理'},
    // '供应商':  { path: '/home',name:'供应商管理'}
  }
  return objPath;
}

/**
 * @description 获取首次登录跳转路径
 * @author 张海峰
 * @date 2020-09-23 10:58:25
 * @param
 * @return
 * */
export const getPath = () => {
  let path = ''
  const strAuthorities = tools.localRead('authorities') || ""
  if (strAuthorities.indexOf('ROLE_Pre_Purchaser') != -1 || strAuthorities.indexOf('ROLE_Pre_Supplier') != -1) {
    path = '/home'
  } else {
    path = '/'
  }
  return path
}

/**
 * @description
 * @author 张海峰
 * @date 2020-10-19 14:01:16
 * @param file 使用before-upload钩子时传入的文件
 * @param {*} arr 限制的扩展名
 * @return
 * */
export const beforeUpload = (file,arr = []) => {
  const typeList = [...arr]
  const fileName = file.name
  const extension = fileName.substr(fileName.lastIndexOf(".") + 1)
  const isRT = typeList.includes(extension)
  return isRT
}

